package com.cqut.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cqut.dto.LimitShowDTO;
import com.cqut.service.IJudgesAppointmentService;

import com.cqut.util.ResponseUtil;

@RestController
@RequestMapping("/api/judgesAppointment")
public class JudgesAppointmentController {
	
    @Resource(name="judgesAppointmentService")
	private IJudgesAppointmentService judgesAppointmentService;

    /**
	 * 初始化大赛列表
	 * @param limitShowDTO
	 * @param errors
	 * @return
	 */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
	public ResponseEntity<String> getList(
			String competeName,
			@Validated LimitShowDTO limitShowDTO,			
            BindingResult errors){ 
        if(errors.hasErrors()) {
	            return ResponseUtil.getResponseEntityWhenInvalidReqParams();
    	}
	    Map<String, Object> result = new HashMap<String, Object>();
	    List<Map<String, Object>> list = judgesAppointmentService.getList(limitShowDTO, competeName);
    	long count = judgesAppointmentService.findCountOfList(competeName);
    	
	    result.put("rows", list);
        result.put("total", count);
    	
	    return ResponseUtil.getResEntityForGetAndJson(result);
	 }
    
    /**
	 * 初始化阶段列表
	 * @param limitShowDTO
	 * @param errors
	 * @return
	 */
    @RequestMapping(value = "/stateList", method = RequestMethod.GET)
	public ResponseEntity<String> getStateList(
			String stateName,
			String competitionId,
			@Validated LimitShowDTO limitShowDTO,			
            BindingResult errors){ 
        if(errors.hasErrors()) {
	            return ResponseUtil.getResponseEntityWhenInvalidReqParams();
    	}
	    Map<String, Object> result = new HashMap<String, Object>();
	    List<Map<String, Object>> list = judgesAppointmentService.getStateList(limitShowDTO, stateName, competitionId);
	    long count = judgesAppointmentService.findCountOfStateList(stateName, competitionId);
    	
	    result.put("rows", list);
        result.put("total", count);
        
	    return ResponseUtil.getResEntityForGetAndJson(result);
	 }
    
    /**
     * 初始化流程列表
     * @param limitShowDTO
	 * @param errors
	 * @return
     * */
    @RequestMapping(value = "/flowList", method = RequestMethod.GET)
   	public ResponseEntity<String> getFlowList(   			
   			String comProcessId,
   			String flowName,
   			@Validated LimitShowDTO limitShowDTO,			
               BindingResult errors){ 
           if(errors.hasErrors()) {
   	            return ResponseUtil.getResponseEntityWhenInvalidReqParams();
       	}
   	    Map<String, Object> result = new HashMap<String, Object>();
   	    List<Map<String, Object>> list = judgesAppointmentService.getFlowList(limitShowDTO, comProcessId, flowName);
   	    long count = judgesAppointmentService.findCountOfFlowList(comProcessId, flowName);
       
   	    result.put("rows", list);
        result.put("total", count);
           
   	    return ResponseUtil.getResEntityForGetAndJson(result);
   	 }
       
    /**
	 * 得到赛制流程列表
	 */
    @RequestMapping(value = "/getFlow", method = RequestMethod.GET)
	public ResponseEntity<String> getFlowStates() {
	    Map<String, Object> result = new HashMap<String, Object>();
	    List<Map<String, Object>> flowStates = judgesAppointmentService.getFlowStates();
	   
	    result.put("rows", flowStates);
	    
	    return ResponseUtil.getResEntityForGetAndJson(result);
	 }
    
    /**
	 * 得到阶段列表
	 */
    @RequestMapping(value = "/getState", method = RequestMethod.GET)
	public ResponseEntity<String> getStates() {
	    Map<String, Object> result = new HashMap<String, Object>();
	    List<Map<String, Object>> StatesList = judgesAppointmentService.getStates();
	   
	    result.put("rows", StatesList);
	    
	    return ResponseUtil.getResEntityForGetAndJson(result);
	 }
    
    /**
     * 得到评委列表
     * */
    @RequestMapping(value = "/getJudges", method = RequestMethod.GET)
	public ResponseEntity<String> judgeslist(
			@Validated LimitShowDTO limitShowDTO,
			String judgeName,
            BindingResult errors){ 
    	 if(errors.hasErrors())
    		 return ResponseUtil.getResponseEntityWhenInvalidReqParams();
	    Map<String, Object> result = new HashMap<String, Object>();
	    List<Map<String, Object>> list = judgesAppointmentService.getJudgesList(limitShowDTO, judgeName);   	
    	long count = judgesAppointmentService.findCountOfJudgesist(judgeName);
    	
    	result.put("rows", list);
        result.put("total", count); 
        
	    return ResponseUtil.getResEntityForGetAndJson(result);       
	 }
    
    /**
     * 指定评委
     * */
    @RequestMapping(value = "/setJudges", method = RequestMethod.POST)
    public ResponseEntity<String> setJudges(
    		@RequestParam("processDetailIds")String[] processDetailIds,
    		@RequestParam("addJudgesIds")String[] addJudgesIds, 
    		@RequestParam("deleteJudgesIds")String[] deleteJudgesIds) {
    	//根据流程id得到process_result_detail_id
    	List<Map<String, String>> states = judgesAppointmentService.selectState(processDetailIds);
    	//根据评委列表得到评委id与评委名字
    	List<Map<String, String>> addJudges = judgesAppointmentService.selectJudges(addJudgesIds);
    	
    	if(!judgesAppointmentService.deleteJudges(deleteJudgesIds, processDetailIds)) {
    		return ResponseUtil.getResEntityForGetWhenFail();
    	} else if(!judgesAppointmentService.setJudges(states, addJudges)) {
    		return ResponseUtil.getResEntityForGetWhenFail();
    	}
    	
    	return ResponseUtil.getResEntityForGetWhenSuccess();
    }
    
    /**
     * 根据大赛id得到流程id
     * */
    @RequestMapping(value = "/getIdsByComp", method = RequestMethod.POST)
	public ResponseEntity<String> getIdsByComp(
			@RequestParam("competitionId")String[] competitionId) {
    	Map<String, Object> result = new HashMap<String, Object>();
    	List<Map<String,Object>> list = judgesAppointmentService.getListByCom(competitionId);
    	
    	result.put("rows", list);
    	
		return ResponseUtil.getResEntityForGetAndJson(result);   	
    }   
    
    /**
     * 根据阶段id得到流程id
     * */
    @RequestMapping(value = "/getIdsByPro", method = RequestMethod.POST)
	public ResponseEntity<String> getIdsByPro(
			@RequestParam("comProcessIds")String[] comProcessIds) {
    	Map<String, Object> result = new HashMap<String, Object>();
    	List<Map<String,Object>> list = judgesAppointmentService.getListByPro(comProcessIds);
    	
    	result.put("rows", list);
    	
		return ResponseUtil.getResEntityForGetAndJson(result);   	
    } 
    
    /**
     * 得到已指定评委
     * */
    @RequestMapping(value = "/getAppointJudges", method = RequestMethod.POST) 
    public ResponseEntity<String> getAppointJudges(
    		@RequestParam("processDetailIds")String[] processDetailIds) {
    	Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> list = judgesAppointmentService.getAppointJudges(processDetailIds);
		
		result.put("rows", list);
		
		return ResponseUtil.getResEntityForGetAndJson(result); 
    }
    
    /**
     * 根据阶段id得到流程id
     * */
    @RequestMapping(value = "/getDetailIds", method = RequestMethod.POST)
	public ResponseEntity<String> getDetailIds(
			@RequestParam("processIds")String[] processIds) {
    	Map<String, Object> result = new HashMap<String, Object>();
    	List<Map<String,Object>> list = judgesAppointmentService.getDetailIds(processIds);
    	
    	result.put("rows", list);
    	
		return ResponseUtil.getResEntityForGetAndJson(result);   	
    } 
}
